package org.example.struct.array;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liyishan
 * @date 2023/8/21 10:26
 * @apiNote
 */

public class Test01160CountCharacters {
    public static void main(String[] args) {

    }

    public static int test(String[] words, String chars) {
        Map<Character,Integer> map = new HashMap<>();
        int length = chars.length();
        for(int i = 0; i < length; i++){
            char c = chars.charAt(i);
            map.put(c,map.getOrDefault(c,0)+1);
        }
        int ans = 0;
        for(String word:words){
            Map<Character,Integer> wordCnt = new HashMap<>();
            int wordLength = word.length();
            for(int i = 0; i < wordLength; i++){
                char c = word.charAt(i);
                wordCnt.put(c,wordCnt.getOrDefault(c,0)+1);
            }
            boolean isAns = true;
            for(int i = 0; i < wordLength; i++){
                char c = word.charAt(i);
                if(map.getOrDefault(c,0)< wordCnt.getOrDefault(c,0)){
                    isAns = false;
                    break;
                }
            }
            if(isAns){
                ans += wordLength;
            }
        }
        return ans;
    }
}
